package pl.edu.pk.mech.kkbus.controller;

import java.util.List;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import pl.edu.pk.mech.kkbus.service.NewsService;
import pl.edu.pk.mech.kkbus.dto.News;
import pl.edu.pk.mech.kkbus.model.ResponseStatus;

@Controller
public class NewsController extends RestController {
    
    @Autowired
    NewsService newService;
	
    private static final Logger log = Logger.getLogger(NewsController.class.getName());

    @RequestMapping(value = "/news", method = RequestMethod.GET)
    public @ResponseBody List<News> getNews() {
        return newService.getAllNews();
    }
    
    @RequestMapping(value = "/news", method = RequestMethod.POST)
    public @ResponseBody ResponseStatus create(@RequestBody News news) {
        log.info("Create news.");
    	boolean result = newService.create(news);
        return result ? ResponseStatus.ADDED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/news/{id}", method = RequestMethod.PUT)
    public @ResponseBody ResponseStatus update(@PathVariable long id, @RequestBody News news) {
        log.info("Update news with id " + id);
        boolean result = newService.update(id, news);
        return result ? ResponseStatus.UPDATED : ResponseStatus.ERROR;
    }
    
    @RequestMapping(value = "/news/{id}", method = RequestMethod.DELETE)
    public @ResponseBody ResponseStatus delete(@PathVariable long id) {
        boolean result = newService.delete(id);
        return result ? ResponseStatus.DELETED : ResponseStatus.ERROR;
    }
}
